/**
 * cn.ucox.web.framework.converter.MapFormConverter
 *
 * @author chenw
 * @create 16/2/12.00:21
 * @email javacspring@hotmail.com
 */

package cn.com.oceansoft.framework.converter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

/**
 * Map表单
 *
 * @author chenw
 * @create 16/2/12 00:21
 * @email javacspring@gmail.com
 */
public class MapFormConverter extends AbstractHttpMessageConverter<Map<String, Object>> {

    private SerializerFeature[] features = new SerializerFeature[0];
    public final static Charset UTF8 = Charset.forName("UTF-8");
    private Charset charset = UTF8;
    public static final int BUFFER_SIZE = 4096;

    public MapFormConverter() {
        super(new MediaType("application", "ucox-api", Charset.forName("UTF-8")));
    }

    @Override
    protected boolean supports(Class clazz) {
        return true;
    }

    @Override
    protected Map<String, Object> readInternal(Class<? extends Map<String, Object>> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
        StringBuilder out = new StringBuilder();
        InputStream in = inputMessage.getBody();
        InputStreamReader reader = new InputStreamReader(in, charset);
        char[] buffer = new char[BUFFER_SIZE];
        int bytesRead;
        while ((bytesRead = reader.read(buffer)) != -1) {
            out.append(buffer, 0, bytesRead);
        }
        String[] result = out.toString().split("\n");




        return new HashMap<String, Object>() {{
            put("head", result[0]);
            put("signal", result[1]);
            put("data", result[2]);
        }};
    }

//    @Override
//    protected Object readInternal(Class<? extends Object> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
//        ByteArrayOutputStream baos = new ByteArrayOutputStream();
//        InputStream in = inputMessage.getBody();
//        byte[] buf = new byte[1024];
//        for (; ; ) {
//            int len = in.read(buf);
//            if (len == -1) {
//                break;
//            }
//            if (len > 0) {
//                baos.write(buf, 0, len);
//            }
//        }
//        byte[] bytes = baos.toByteArray();
//        return JSON.parseObject(bytes, 0, bytes.length, charset.newDecoder(), clazz);
//    }


    @Override
    protected void writeInternal(Map<String, Object> data, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
        OutputStream out = outputMessage.getBody();
        String text = JSON.toJSONString(data, features);
        byte[] bytes = text.getBytes(charset);
        out.write(bytes);
    }

//    @Override
//    protected void writeInternal(Object data, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
//        OutputStream out = outputMessage.getBody();
//        String text = JSON.toJSONString(data, features);
//        byte[] bytes = text.getBytes(charset);
//        out.write(bytes);
//    }
}
